from PySide6.QtWidgets import QWidget, QHBoxLayout, QLabel,QPushButton
from PySide6.QtCore import Qt, QSize
from PySide6.QtGui import QPixmap, QFont
from signalObject import windowSignal

class windowIcon(QLabel):
    def __init__(self,parent=None):
        super().__init__(parent)

        self.initSetup()

    def initSetup(self):
        self.setObjectName('windowIcon')
        self.setFixedSize(50,50)
        self.setPixmap(QPixmap('./img/terminal.png'))
        self.setAlignment(Qt.AlignmentFlag.AlignCenter)

class actionsBar(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._press = False

        self.close = closeIcon()
        self.minning = minIcon()
        self.historyIcon = QPushButton()
        self.setIcon = QPushButton()
        self.shareIcon = QPushButton()

        self.windowTopLeftPos = None
        self.mouseStartPos = None


        self.initSetup()

    def initSetup(self):
        from core.setting import WINDOWTITLE

        self.setObjectName('topAction')

        self.setMaximumSize(600, 50)
        self.setMinimumSize(0, 0)
        self.resize(600, 50)

        self.setAttribute(Qt.WA_StyledBackground, True)  # 支持qss设置背景
        self.setCursor(QPixmap('./img/mouse4.png'))

        layout = QHBoxLayout(self)

        title = QLabel(WINDOWTITLE)
        title.setFont(QFont('微软雅黑', 15))

        self.historyIcon.setFixedSize(36,36)
        self.setIcon.setFixedSize(36, 36)
        self.shareIcon.setFixedSize(36, 36)
        self.historyIcon.setIconSize(QSize(36,36))
        self.setIcon.setIconSize(QSize(36, 36))
        self.shareIcon.setIconSize(QSize(36, 36))

        self.historyIcon.setIcon(self.getIconPixmap('./img/history.png'))
        self.setIcon.setIcon(self.getIconPixmap('./img/set.png'))
        self.shareIcon.setIcon(self.getIconPixmap('./img/share.png'))


        layout.addWidget(title, alignment=Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        layout.addSpacing(500)

        layout.addWidget(self.shareIcon, alignment=Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        layout.addWidget(self.setIcon, alignment=Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        layout.addWidget(self.historyIcon, alignment=Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        layout.addWidget(self.minning, alignment=Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        layout.addWidget(self.close, alignment=Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)

        layout.setContentsMargins(0, 3, 10, 5)

    @classmethod
    def getIconPixmap(cls, path: str, width: int = 30, height: int = 30):
        return QPixmap(path).scaled(width,
                                    height,
                                    Qt.AspectRatioMode.IgnoreAspectRatio,
                                    Qt.TransformationMode.SmoothTransformation)

class iconLabel(QLabel):
    def __init__(self, iconPath:str, iconWidth=30, iconHeight=30, parent=None):
        super().__init__(parent)
        self.iconPath = iconPath
        self.iconWidth, self.iconHeight = iconWidth,iconHeight

        self._press = False
        self.initSetup()

    def initSetup(self):
        self.setPixmap(actionsBar.getIconPixmap(self.iconPath,width=self.iconWidth,height=self.iconHeight))
        self.setGeometry(0, 0, self.iconWidth, self.iconHeight)

    def mousePressEvent(self, ev):
        if ev.button() == Qt.MouseButton.LeftButton:
            self._press = True

    def mouseReleaseEvent(self, ev):
        if not (0 <= ev.pos().x() <= self.width() and 0 <= ev.pos().y() <= self.height()):
            self._press = False


class closeIcon(iconLabel):
    def __init__(self, iconPath='./img/off.png', parent=None):
        super().__init__(iconPath,parent=parent)
        self.signalObject = windowSignal()

    def mouseReleaseEvent(self, ev):
        super().mouseReleaseEvent(ev)
        if self._press:
            self.signalObject.closeWindow.emit()

class minIcon(iconLabel):
    def __init__(self, iconPath='./img/min.png', parent=None):
        super().__init__(iconPath,parent=parent)
        self.signalObject = windowSignal()

    def mouseReleaseEvent(self, ev):
        super().mouseReleaseEvent(ev)
        if self._press:
            self.signalObject.minWindow.emit()
